<?php
/**
 * Name
 * 2018/11/5
 * ╔═════╗                 ╔═╗ ╔═╗
 * ║ ╔═══╝                 ║ ║ ║ ║
 * ║ ╚═══╦══╦══╦═════╦═════╣ ╚═╝ ║
 * ╚═══╗ ║  ║  ║  ║  ║  ║  ╠═╗ ╔═╝
 * ╔═══╝ ║  ║  ║  ║  ║  ║  ║ ║ ║
 * ╚═════╩═════╩══╩══╩══╩══╝ ╚═╝
 */

namespace App\Models;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class Order extends Authenticatable
{
    use HasRoles;
    protected $guard_name = 'web';
    protected $table = 'order';
    public $timestamps = false;
    protected $rememberTokenName = '';
    protected $fillable = [
        'username',
        'password',
        'realname',
        'phone',
        'status',
        'role',
        'region',
        'created_at',
        'updated_at'
    ];

    // 订单类型
    const TYPE_ONLINE = 1; // 线上发货
    const TYPE_MACHINE = 2; // 售卖机取货
    const TYPE_SCANCODE = 3; // 扫码支付订单
    const TYPE = [
        self::TYPE_ONLINE => "线上发货",
        self::TYPE_MACHINE => "售卖机取货",
        self::TYPE_SCANCODE => "扫码支付订单"
    ];
    // 订单状态
    const STATUS_WAIT_PAY = 1; // 待付款
    const STATUS_WAIT_DELIVER = 2; // 待发货
    const STATUS_WAIT_COLLECT = 3; // 待收货
    const STATUS_WAIT_GET = 4; // 待取货
    const STATUS_SUCCESS = 5; // 已完成
    const STATUS_AFTER_ING = 6; // 申请售后中
    const STATUS_CANCEL = 7; // 取消
    const STATUS_REFUND = 8; // 退款
    const STSTUS = [
         self::STATUS_WAIT_PAY => '待付款' ,
         self::STATUS_WAIT_DELIVER => '待发货' ,
         self::STATUS_WAIT_COLLECT => '待收货' ,
         self::STATUS_WAIT_GET => '待取货' ,
         self::STATUS_SUCCESS => '已完成' ,
         self::STATUS_AFTER_ING =>'申请售后中',
         self::STATUS_CANCEL =>'已取消',
         self::STATUS_REFUND =>'已退款 '
    ];
    // 付款方式
    const PAY_TYPE_WX = 1; // 微信支付
    const PAY_TYPE_SELF = 2; // 余额支付
    const PAY_TYPE_BABEI = 3; // 八贝支付
    const PAY_TYPE = [
        self:: PAY_TYPE_WX    => '微信支付',
        self:: PAY_TYPE_SELF  => '余额支付',
        self:: PAY_TYPE_BABEI => '八贝支付'
    ];
    // 邮寄状态
    const POST_STATUS_IS = 1;
    const POST_STATUS_NO = 2;
    const POST_STATUS = [
        self::POST_STATUS_IS => '已发货',
        self::POST_STATUS_NO => '未发货'
    ];
    //邮寄方式
    const POST = 1 ;
    const FETCH = 2 ;
    const SEND_TYPE = [
        self::POST => '邮寄',
        self::FETCH => '自取'
    ];



    //快递公司
    const EMS = 'ems';
    const YTKD = 'yuantong';
    const YDKD = 'yunda';
    const SFKD = 'shunfeng';
    const STKD = 'shentong';
    const ZTKD = 'zhongtong';
    const JDWL = 'jd';
    const EXPRESS = [
        self::YDKD => '韵达快递',
        self::ZTKD => 'EMS',
        self::YTKD => '圆通快递',
        self::SFKD => '顺丰快递',
        self::STKD => '申通快递',
        self::STKD => '京东物流',
        self::ZTKD => '中通快递',
    ];

    const CREATE = 1;
    const WITHDRAW = 2;
    const ACCEPT = 3;
    const REFUSE = 5;
    const EXPRESS_STATUS = [
        self::CREATE => '订单创建',
        self::WITHDRAW => '订单已取消',
        self::ACCEPT => '接单成功',
        self::REFUSE => '地址不送达',

    ];
    const PARTNERID = 200064123456;
    const PASSWORD = 'arXC2ZKnAcfg6Myh8vRpEdDT5FIGme';
    const VERSION = '1.4';

    public function insert($data)
    {
        return $this->insertGetId($data);
    }

    public function edit($id, $data)
    {
        if($this->find($id)){
            return DB::table($this->table)->where('id', $id)->update($data);
        }
    }

    public function del($id)
    {
        return $this->where('id', $id)->delete();
    }

    public function find($id)
    {
        return $this->where('id', $id)->first()->toArray();
    }

    public function getAll()
    {
        return $this->get()->toArray();
    }

    public function order_goods(){
        return $this->hasMany('App\Models\order_goods','goods_id','id')->select('model_id','role_id')->withDefault([
           'model_id' => -1,
           'role_id' => ''
        ]);
    }

    public function orderGoods()
    {
        return $this->hasMany('App\Models\OrderGoods','order_no','online_order');
    }
    public function orderGood()
    {
        return $this->hasOne('App\Models\OrderGoods','order_no','online_order');
    }

    public function User()
    {
       return $this->belongsTo('App\Models\User','user_id','id');
    }
    public function userInfo(){
        return $this->hasOne('App\Models\User','id','user_id');
    }











}